import { Style, Fill, Stroke, Text, Icon } from 'ol/style';
import { colorRgb } from '../util'
import { mapManager } from '../MapManager';
import Circle from 'ol/style/Circle';
import { Point, MultiPoint } from 'ol/geom'

const TUNNEL_Z = 720;

/**
 * 地块样式的封装类
 */
class FieldStyle {
    static defaultColor = '#ADE6B1';
    /**
     * 获取地块样式
     * @public
     * @param {import("../../../util/typedef").FieldInfo} field   地块信息
     * @return {import('ol/style/Style').StyleFunction} 返回样式
     */
    static getStyle(field) {

        return function (feature) {


            var lineDash = null;
            var opacity = field.opacity;
            var fontSize = 12;
            var scale = 0.5;
            var strokeWidth = 1;
            var zoom = mapManager.getMap().getView().getZoom();
            var color = field.viewColor == "" || field.viewColor == null ? FieldStyle.defaultColor : field.viewColor;
            var fillColor = color;
            var offsetY = 24;

            var lineDashed = feature.get('lineDashed'); //获取属性，是否为虚线
            if (lineDashed) {
                lineDash = [10, 10];
            }

            if (zoom >= 18) {
                opacity = 0;
            }


            if (zoom < 16) {
                scale = 0.4;
                fontSize = 11;
                offsetY = 16;
            }

            if (zoom < 15.1) {
                scale = 0;
                fontSize = 10;
                offsetY = 12;

                fontSize = 0;
            }


            if (zoom < 14.1) {
                scale = 0;
                fontSize = 0;

                offsetY = 1;
            }

            if (zoom < 13.5) {

                fontSize = 0;
                scale = 0;
                strokeWidth = 1;
            }

            if (zoom < 13.2) {
                opacity = 0;
                fontSize = 0;
                scale = 0;
                strokeWidth = 0.1;
            }


            //设置样式

            var style = new Style({
                stroke: new Stroke({
                    color: color,
                    width: strokeWidth,
                    lineDash: lineDash
                }),
                fill: new Fill({
                    color: "rgba(" + colorRgb(fillColor) + ", " + opacity + ")"
                }),
                text: new Text({
                    font: fontSize + 'px sans-serif',
                    text: field.name,
                    offsetY: offsetY,
                })
            });


            var styles = [style, new Style({
                geometry: function (feature) {
                    return feature.getGeometry().getInteriorPoint();
                },
                image: new Icon({
                    scale: scale,
                    src: imgurl
                }),
            })];


            if(lineDashed){
                styles.push(borderPointStyle1);
                styles.push(borderPointStyle2);
            }

            return styles;
        }


    }

    /**
    * 地块规划中polygon 样式
    */
    static drawFieldStyle() {
        return [new Style({
            stroke: new Stroke({
                color: FieldStyle.defaultColor,
                lineDash: [10, 10],
                width: 2
            }),
            fill: new Fill({
                color: "rgba(173,230,177, 0.45)"
            }),
            zIndex: TUNNEL_Z
        }),
        
        borderPointStyle1,borderPointStyle2
        ];
    }


}

const borderPointStyle1 = new Style({
    image: new Circle({
        radius: 5,
        fill: new Fill({
            color: '#fff'
        })
    }),
    geometry: function (feature) {
        var type = feature.getGeometry().getType();
        var coordinates = feature.getGeometry().getCoordinates()
        if(type == 'Point'){
            return new MultiPoint([coordinates]);
        }else{
          return new MultiPoint(coordinates[0]);
        }
    }
});


const borderPointStyle2 =   new Style({
    image: new Circle({
        radius: 5,
        fill: new Fill({
            color: 'yellow'
        })
    }),
    geometry: function (feature) {
        var type = feature.getGeometry().getType();
        var coordinates = feature.getGeometry().getCoordinates()
        if(type == 'Point'){
            return new Point(coordinates);
        }else{
          return new Point(coordinates[0][0]);
        }
    }
});


export default FieldStyle;